package com.smarthome.web.property.controller.system;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.smarthome.core.propertystaffmanager.PropertyCompanyService;
import com.smarthome.domain.bean.ShPropertyCompany;
import com.smarthome.domain.common.Result;
import com.smarthome.domain.vo.boss.DataGrid;
import com.smarthome.domain.vo.boss.PageHelper;
import com.smarthome.web.property.utils.UserHolder;

/**
 * @author dhc
 * @title 物业公司管理
 * @date 2015/09/12
 */
@Controller
@RequestMapping("/propertyCompanyController")
public class PropertyCompanyController {
	private Logger logger = Logger.getLogger(PropertyCompanyController.class);
	@Resource(name="propertyCompanyService")
    private PropertyCompanyService propertyCompanyService;
	
	/**
	 * @param company
	 * @param helper
	 * @param request
	 * @return
	 */
	@RequestMapping("/dataGrid")
    @ResponseBody
	public DataGrid getPage(ShPropertyCompany company,PageHelper helper,HttpServletRequest request){
		if(null == company){
			company =new ShPropertyCompany();
		}
		
		if(!UserHolder.isSuperResidence()){
			company.setResidenceId(UserHolder.getCurrentResidenceId());
		}
		
		return propertyCompanyService.qeuryDataGrid(company, helper);
	}
	
	@RequestMapping("/addPage")
    public String addPage(HttpServletRequest request) {
		
		
        return "/system/propertystaffmanager/propertyCompanyAdd";
	}
	@RequestMapping("/editPage")
	public String eidtPage(HttpServletRequest request,Long id) {
		request.setAttribute("company", propertyCompanyService.queryShPropertyCompanyById(id));
		
		return "/system/propertystaffmanager/propertyCompanyEdit";
	}
	
	  @RequestMapping("/add")
	  @ResponseBody
	  public Result add(ShPropertyCompany company) {
		  if(StringUtils.isBlank(company.getName())){
			  return Result.failure("添加失败，公司名字为必填！");
		  }
		  if(StringUtils.isBlank(company.getAddress())){
			  return Result.failure("添加失败，公司地址为必填！");
		  }
		  
		  propertyCompanyService.add(company);
	      return Result.success("添加成功！");
	    }
	  
	  @RequestMapping("/edit")
	  @ResponseBody
	  public Result edit(ShPropertyCompany company) {
		  
		  if(StringUtils.isBlank(company.getName())){
			  return Result.failure("编辑失败，公司名字为必填！");
		  }
		  if(StringUtils.isBlank(company.getAddress())){
			  return Result.failure("编辑失败，公司地址为必填！");
		  }
		  
		  propertyCompanyService.edit(company);
		  return Result.success("编辑成功！");
	  }
	  
	  @RequestMapping("/delete")
	  @ResponseBody
	  public Result delete(Long id) {
		  try{
			   propertyCompanyService.delete(id);
		  } catch(IllegalAccessError e){
			  logger.info(e.getMessage());
			  return Result.failure("公司被引用，不能删除");
		  }
		  return Result.success("删除成功！");
	  }
	
}
